//
//  PersonInfoVC.m
//  FamilyTree
//
//  Created by student on 16/4/12.
//  Copyright © 2016年 Five. All rights reserved.
//

#import "PersonInfoVC.h"
#import "HeadButton.h"
#import "PeopleDataManager.h"
#import "People.h"
#define kScreenBoundsWidth [UIScreen mainScreen].bounds.size.width
#define kScreenBoundsHeight [UIScreen mainScreen].bounds.size.height
@interface PersonInfoVC ()<UIAlertViewDelegate,UIImagePickerControllerDelegate>
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIView *briefView;//简介
@property (nonatomic,weak) UIButton *headBtn;
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) NSArray *arr;
@property (nonatomic,strong) UIView *moveView;
@property (nonatomic,strong) UIScrollView *infoScrollView;
@end

@implementation PersonInfoVC
- (UIView *)briefView
{
    if (_briefView == nil) {
        _briefView = [[UIView alloc]initWithFrame:CGRectMake(0, 44, kScreenBoundsWidth, kScreenBoundsHeight *0.14)];
        _briefView.backgroundColor = [UIColor grayColor];
        //头像
        HeadButton *headBtn = [[HeadButton alloc]initWithFrame:CGRectMake(5, 0, kScreenBoundsHeight *0.14, kScreenBoundsHeight *0.14)];
        _headBtn = headBtn;
        //加载首先访问本地沙盒是否存在相关图片
        NSString * fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",_name]];
        UIImage *saveImage = [UIImage imageWithContentsOfFile:fullPath];
        if (!saveImage) {
            //默认头像
            [_headBtn setImage:[UIImage imageNamed:@"userdata"] forState:UIControlStateNormal];
        }
        else
        {
            [_headBtn setImage:saveImage forState:UIControlStateNormal];
        }
        [_headBtn addTarget:self action:@selector(UesrImageClicked) forControlEvents:UIControlEventTouchUpInside];
        [_briefView addSubview:_headBtn];
        //名字
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenBoundsHeight *0.14+10, 10, kScreenBoundsWidth/2, kScreenBoundsHeight*0.14-20)];
        _nameLabel.font = [UIFont systemFontOfSize:20];
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.text = @"迈克尔刘能";
        _nameLabel.textAlignment = UITextAlignmentCenter;
        _arr = [[PeopleDataManager sharePeopleDataManager] quaryAllPeopleInfo];
        for(People *p in _arr)
        {
            if ([p.name isEqualToString: @""])
            {
                _nameLabel.text = @"迈克尔刘能";
            }
            else if([p.name isEqualToString:self.name])
            {
                _nameLabel.text = p.name;
            }
        }
        [_briefView addSubview:_nameLabel];
    }
    return _briefView;
}
- (void)UesrImageClicked
{
    UIActionSheet *sheet;
    
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        sheet  = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照", @"从相册选择", nil];
    }
    else {
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];
    }
    
    sheet.tag = 255;
    
    [sheet showInView:self.view];
}


#pragma mark - action sheet delegte
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 255) {
        NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            switch (buttonIndex) {
                case 0:
                    return;
                case 1: //相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 2: //相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
            }
        }
        else {
            if (buttonIndex == 0) {
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
        // 跳转到相机或相册页面
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = sourceType;
        
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }
}

#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = info[@"UIImagePickerControllerEditedImage"];
    //保存在沙盒里
    [self saveImage:image withName:[NSString stringWithFormat:@"%@.png",self.name]];
    self.headBtn.imageView.image = image;
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 保存图片至本地沙盒
- (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.8);
    
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}
// 取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - scrollView
- (UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 44+kScreenBoundsHeight*0.14+1, kScreenBoundsWidth, kScreenBoundsHeight*0.1)];
        NSArray *arr = @[@"信息",@"配偶",@"父母",@"相片",@"故事"];
        for(int i =0;i<5;i++)
        {
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(i*kScreenBoundsWidth/5, 0, kScreenBoundsWidth/5, kScreenBoundsHeight*0.1)];
            if (i == 0)
            {
                btn.selected = YES;
            }
            [btn setTitle:arr[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            btn.titleLabel.textAlignment = UITextAlignmentCenter;
            btn.titleLabel.adjustsFontSizeToFitWidth = YES;
            [btn setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
            btn.tag = 100+i;
            [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
            [_scrollView addSubview:btn];
        }
        //滑动块
        _moveView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenBoundsHeight*0.1-5, kScreenBoundsWidth/5, 5)];
        _moveView.backgroundColor = [UIColor greenColor];
        [_scrollView addSubview:_moveView];
    }
    return _scrollView;
}
- (UIScrollView *)infoScrollView
{
    int upHeight = (44+kScreenBoundsHeight*0.14+1) + (kScreenBoundsHeight*0.1);
    if (_infoScrollView == nil) {
        _infoScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, upHeight, kScreenBoundsWidth*5, kScreenBoundsHeight - upHeight)];
        
        
    }
    return _infoScrollView;
}
- (void)btnPressed:(UIButton *)sender
{
    for (UIView *v in self.scrollView.subviews)
    {
        if([v isKindOfClass:[UIButton class]])
        {
            UIButton *tmpBtn = v;
            tmpBtn.selected = NO;
        }
    }
    // 2. 将当前按的按钮设置成选中状态
    sender.selected = YES;
    
    // 3. 移动滑块
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.1];
    int index = sender.tag - 100;
    self.moveView.frame = CGRectMake(kScreenBoundsWidth/5*index, kScreenBoundsHeight*0.1-5, kScreenBoundsWidth/5, 5);
    [UIView commitAnimations];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view addSubview:self.briefView];
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.infoScrollView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
